package com.beemans.common.ui.fragments

import android.os.Bundle
import android.view.View
import androidx.annotation.CallSuper
import com.beemans.common.base.ICommonUIAction
import com.beemans.common.ext.immersionBarExt
import com.beemans.common.ext.showCallback
import com.beemans.common.ui.activities.CommonActivity
import com.beemans.common.ui.loadsir.EmptyCallback
import com.beemans.common.ui.loadsir.ErrorCallback
import com.beemans.common.ui.loadsir.LoadingCallback
import com.beemans.common.utils.UMConfig
import com.gyf.immersionbar.ImmersionBar
import com.kingja.loadsir.core.LoadService
import com.tiamosu.fly.base.BaseFlyFragment
import com.tiamosu.fly.ext.addObserve

/**
 * @author tiamosu
 * @date 2021/3/15
 */
abstract class CommonFragment : BaseFlyFragment(), ICommonUIAction {
    internal var loadService: LoadService<Any>? = null  // 多状态布局

    private var isNetRequestRefresh = false    //是否进行网络数据刷新

    internal var jumpRunnable: Runnable? = null //页面跳转

    /**
     * 友盟路劲上报，页面路劲名称
     */
    open val onPageFragmentTag: String = fragmentTag

    final override fun isCheckNetChanged() = isRefreshNetRequest()

    /**
     * 是否开启网络状态变化监听
     */
    open fun isRefreshNetRequest() = false

    /**
     * 网络数据请求刷新
     */
    @CallSuper
    open fun refreshNetRequest() {
        isNetRequestRefresh = false
    }

    @CallSuper
    override fun onNetReConnect() {
        isNetRequestRefresh = true
        if (isFlySupportVisible()) {
            refreshNetRequest()
        }
    }

    /**
     * 沉浸式状态栏配置
     */
    open fun statusBarConfig(block: ImmersionBar.() -> Unit = {}) {
        immersionBarExt(block)
    }

    /**
     * 页面传参获取，数据初始化
     */
    override fun initParameters(bundle: Bundle?) {}

    /**
     * 视图初始化
     */
    override fun initView(rootView: View?) {}

    /**
     * 监听事件初始化
     */
    override fun initEvent() {}

    /**
     * LiveData 观察者注册，配合[addObserve]使用
     */
    override fun createObserver() {}

    override fun onFlySupportVisible() {
        super.onFlySupportVisible()
        if (post(jumpRunnable)) {
            jumpRunnable = null
        }
        statusBarConfig()
        UMConfig.onPageFragmentStart(onPageFragmentTag)

        if (isNetRequestRefresh) {
            refreshNetRequest()
        }
    }

    override fun onFlySupportInvisible() {
        super.onFlySupportInvisible()
        UMConfig.onPageFragmentEnd(onPageFragmentTag)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        jumpRunnable = null
    }

    override fun showToast(msg: String?) {
        (context as? CommonActivity)?.showToast(msg)
    }

    override fun showViewLoading() {
        loadService?.showCallback<LoadingCallback>()
    }

    override fun showViewSuccess() {
        loadService?.showSuccess()
    }

    override fun showViewEmpty() {
        loadService?.showCallback<EmptyCallback>()
    }

    override fun showViewError() {
        loadService?.showCallback<ErrorCallback>()
    }
}